<template>
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="primary" icon="el-icon-plus" size="small" @click.stop="handleSave">新 增</el-button>
      </template>

      <template slot-scope="scope" slot="menu">
        <el-button icon="el-icon-edit" size="small" type="text" @click.stop="handleEdit(scope.row, scope.index)">编 辑</el-button>
      </template>

      <template slot-scope="{ disabled, size }" slot="beginTimeSearch">
        <el-date-picker
          v-model="search.beginTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          @change="beginTimeChange"
          placeholder="请输入 扣费开始日期"
        ></el-date-picker>
      </template>
      <template slot-scope="{ disabled, size }" slot="endTimeSearch">
        <el-date-picker
          v-model="search.endTime"
          type="datetime"
          default-time="23:59:59"
          value-format="yyyy-MM-dd HH:mm:ss"
          @change="endTimeChange"
          placeholder="请输入 扣费结束日期"
        ></el-date-picker>
      </template>

      <!-- <template slot-scope="{}" slot="rpiNoSearch">
        <el-select v-model="search.rpiNo" multiple placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </template> -->

    </avue-crud>
    <el-dialog
      :title="dialogText"
      top="4%"
      :width="screenWidth>1920?'30%':(screenWidth<=1920 && screenWidth>1500)?'50%':'80%'"
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      v-if="dialogVisible"
      :before-close="handleClose"
    >
      <el-form :model="addForm" :rules="rules" ref="addForm" size="small" label-width="140px" class="demo-addForm">
        <div class="f_s16">生效规则</div>
        <el-form-item label="政策名称" prop="name">
          <el-input v-model="addForm.name" class="haruyuki-width-50"></el-input>
        </el-form-item>
        <el-form-item label="服务商开通后" prop="name">
          <el-input-number
            class="haruyuki-width-50"
            v-model="addForm.ss"
            :min="0"
            controls-position="right"
          ></el-input-number>
          <span class="p_l10">天生效</span>
        </el-form-item>
        <el-form-item label="服务商本级当天交易" prop="name">
          <el-input-number
            class="haruyuki-width-50"
            v-model="addForm.dd"
            :min="0"
            controls-position="right"
          ></el-input-number>
          <span class="p_l10">笔后生效</span>
        </el-form-item>
        <el-form-item label="服务商白名单">
          <el-button type="primary" @click="serviceDialog = true">点击查看或设置</el-button>
        </el-form-item>
        <el-form-item label="是否生效" prop="resource">
          <el-radio-group v-model="addForm.resource">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <div  class="f_s16">计算规则</div>
        <el-form-item label="优化日结" prop="name">
          <el-input-number
            class="haruyuki-width-50"
            v-model="addForm.dd"
            :min="0"
            :max="100"
            controls-position="right"
          ></el-input-number>
          <span class="p_l10">%，分润到钱包</span>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="() => { dialogVisible = false; initEmpty(); }">取 消</el-button>
        <el-button style="margin-left:10px;" size="small" type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- :width="screenWidth>1920?'30%':(screenWidth<=1920 && screenWidth>1500)?'50%':'80%'" -->
    <el-dialog
      title="服务商白名单"
      top="3%"
      width="96%"
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="serviceDialog"
      v-if="serviceDialog"
    >
      <serviceWhite ref="serviceWhite"/>
    </el-dialog>
  </basic-container>
</template>

<script>
import { getList } from "@/api/rb-oem/serviceTemplate/service_template";
import { mapGetters } from "vuex";
import selectPage from "@/components/select-page/select-page";
import serviceWhite from "./components/wallet/serviceWhite";
import merchantWhite from "./components/wallet/merchantWhite";

export default {
  components: { selectPage,serviceWhite,merchantWhite },
  data() {
    return {
      screenWidth: document.documentElement.clientWidth,
      query: {},
      search: {},
      loading: true,
      dialogText: '',
      dialogType: '',
      dialogVisible: false,
      
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        selection: false,
        searchSpan: 6,
        // searchMenuSpan: 6,//搜索按钮所占比例
        columnBtn: false,
        addBtn: false,
        editBtn: false,
        delBtn:false,
        searchLabelWidth: 120, //搜索框标题宽度
        menuWidth:100,
        menu:false,
        column: [
          {
            label: "政策编号",
            prop: "polNo",
            type: "input",
            overHidden: true,
          },
          {
            label: "政策名称",
            prop: "polName",
            type: "input",
            search: true,
            overHidden: true,
          },
          {
            label: "是否生效",
            searchLabel: "生效状态",
            prop: "polType",
            type: "select",
            search: true,
            dicUrl: "/api/blade-system/dict/dictionary?code=yes_or_no",
            dataType: "number",
            slot: true,
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            overHidden: true,
          },
          {
            label: "创建时间",
            prop: "createTime",
            type: "input",
            display: false,
            overHidden: true,
          },
          {
            label: "创建开始日期",
            prop: "beginTime",
            // type: "datetime",
            // format:'yyyy-MM-dd HH:mm:ss',
            // valueFormat:'yyyy-MM-dd HH:mm:ss',
            // defaultTime:"00:00:00",
            search: true,
            hide: true,
            display: false
          },
          {
            label: "创建结束日期",
            prop: "endTime",
            // type: "datetime",
            // format:'yyyy-MM-dd HH:mm:ss',
            // valueFormat:'yyyy-MM-dd HH:mm:ss',
            // defaultTime:"23:59:59",
            search: true,
            hide: true,
            display: false
          }
        ],
      },
      data: [],
      addForm:{
        resource:1
      },
      serviceDialog:false
      
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        viewBtn: this.vaildData(this.permission.deal_view, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  methods: {
    beginTimeChange() {
      if(this.search.endTime){
        if(Date.parse(this.search.beginTime) > Date.parse(this.search.endTime)){
          this.$alert('开始日期不能大于结束日期', '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
          .then(() => (this.search.beginTime = ""))
          .catch(() => (this.search.beginTime = ""))
        }
      }
    },
    endTimeChange() {
      if(this.search.beginTime){
        if(Date.parse(this.search.endTime) < Date.parse(this.search.beginTime)){
          this.$alert('结束日期不能小于开始日期', '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
          .then(() => (this.search.endTime = ""))
          .catch(() => (this.search.endTime = ""))
        }
      }
    },
    // 提交保存
    submit() {
      
    },
    handleSave() {
      this.dialogVisible = true;
      this.dialogType = 'add';
      this.dialogText = "新增";
      // this.policyForm.id = null;
    },
    handleEdit(row) {
      this.dialogVisible = true;
      this.dialogType = 'edit';
      this.dialogText = "编辑";
      // getDetail({
      //   id: row.id
      // }).then((res) => {
      //   this.$refs['toPolicyTemplate'].getEditDatail(res.data.data.policyList);
      //   this.policyForm.polNo = row.polNo;
      //   this.policyForm.id = res.data.data.id;
      //   this.policyForm.polName = res.data.data.polName;
      //   this.policyForm.polType = res.data.data.polType + '';
      // });
    },
    // handleClose(done) {
    //   this.initEmpty();
    //   done()
    // },
    // initEmpty() {
    //   this.$refs['toPolicyTemplate'].$refs['policyForm'].resetFields();
    //   this.$refs['toPolicyTemplate'].initData = [];
    //   this.$refs['toPolicyTemplate'].data = [];
    //   this.$refs['toPolicyTemplate'].aisleData = null;
    //   this.$refs['toPolicyTemplate'].aptTemplate = {
    //     aptRate: 0,
    //     aptScale: 0,
    //     aptMax: 0,
    //   }
    // },
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        // getDetail(this.form.id).then((res) => {
        //   this.form = res.data.data;
        // });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        // this.selectionClear();
      });
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        //倒序
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    }
  }
};
</script>

<style lang="scss" scoped>
.f_s16{
  font-size: 16px;
}
.p_b4{
  padding-bottom: 4px;
}
.p_l10{
  padding-left: 10px;
}

/deep/ .el-dialog__body{
  padding-top: 0 !important;
}

/deep/ .el-input-number__decrease,
/deep/ .el-input-number__increase{
  display: none;
}
/deep/ .el-input-number.is-controls-right .el-input__inner{
  padding-right:15px;
  text-align: left;
}
</style>
